package com.macro.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.User;
import com.macro.mall.portal.service.IBillLadingService;
import com.macro.mall.portal.dao.BillLadingMapper;
import com.macro.mall.portal.domain.BillLading;
import com.macro.mall.portal.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 提单记录表 服务实现类
 * </p>
 *
 * @author db
 * @since 2021-07-04
 */
@Service
public class BillLadingServiceImpl extends ServiceImpl<BillLadingMapper, BillLading> implements IBillLadingService {

    @Autowired
    private BillLadingMapper billLadingMapper;
    @Autowired
    private IUserService userService;

    @Override
    public CommonResult addOne(BillLading billLading) {
        //判断是否抢单
        QueryWrapper<BillLading> wrapper = new QueryWrapper<>();
        wrapper.eq("i_phone", billLading.getIPhone());
        List<BillLading> ladings = billLadingMapper.selectList(wrapper);
        //推荐人信息
        User user = userService.userInfo(billLading.getOpenId());
        if (!StringUtils.isEmpty(ladings)) {
            for (BillLading bill : ladings) {
                if (bill.getSuperId() != user.getId()) {
                    return CommonResult.error(203, "推荐人已经绑定上级!");
                }
            }
        }
        if (StringUtils.isEmpty(billLading.getMoney())) {
            billLading.setIncome(new BigDecimal(3000));
        } else if (!StringUtils.isEmpty(billLading.getMoney())) {
            BigDecimal money = new BigDecimal(billLading.getMoney()).multiply(new BigDecimal(10000));
            billLading.setIncome(money.multiply(new BigDecimal(0.005)));
        }

        BillLading lading = billLading.toBuilder().superId(user.getId()).referee(user.getAccount()).build();
        billLadingMapper.insert(lading);

        return CommonResult.success(null, "添加成功！");
    }

    @Override
    public IPage<BillLading> queryPage(String openId, Integer status, Integer page, Integer size) {
        QueryWrapper<BillLading> wrapper = new QueryWrapper<>();
        User user = userService.userInfo(openId);
        wrapper.eq("super_id", user.getId());
        wrapper.eq("status", status);
        wrapper.orderByDesc("create_time");
        Page<BillLading> userPage = new Page<>(page, size);
        IPage<BillLading> billLadingIPage = billLadingMapper.selectPage(userPage, wrapper);
        return billLadingIPage;
    }

    @Override
    public Map<String, Object> invite(String openId, Integer status, Integer page, Integer size) {
        QueryWrapper<BillLading> wrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        User user = userService.userInfo(openId);
        if (StringUtils.isEmpty(user)) {
            map.put("msg：", "用户不存在！");
            return map;
        }
        wrapper.eq("super_id", user.getId());
        wrapper.eq("status", status);
        wrapper.orderByDesc("create_time");
        PageHelper.startPage(page, size);
        List<BillLading> selectList = billLadingMapper.selectList(wrapper);
        map.put("selectList", selectList);
        Map<String, Object> above = above(user);
        map.put("total", above.get("total"));
        map.put("income", above.get("income"));
        map.put("numberPro", above.get("numberPro"));
        map.put("numberSes", above.get("numberSes"));
        map.put("numberError", above.get("numberError"));
        return map;
    }

    public Map<String, Object> above(User user) {
        Map<String, Object> map = new HashMap<>();
        BigDecimal reduce = null;
        QueryWrapper<BillLading> success = new QueryWrapper<>();
        success.eq("super_id", user.getId());
        List<BillLading> billLadings = billLadingMapper.selectList(success);
        if (!StringUtils.isEmpty(billLadings)) {
            //成功收益
            reduce = billLadings.stream().filter(s -> s.getStatus() == 2).map(BillLading::getIncome).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        List<BillLading> numberPro = billLadings.stream().filter(s -> s.getStatus() == 1).collect(Collectors.toList());
        List<BillLading> numberSes = billLadings.stream().filter(s -> s.getStatus() == 2).collect(Collectors.toList());
        List<BillLading> numberEro = billLadings.stream().filter(s -> s.getStatus() == 3).collect(Collectors.toList());

        map.put("total", numberSes == null ? 0 : billLadings.size());
        map.put("income", reduce == null ? 0 : reduce);
        map.put("numberPro", numberPro == null ? 0 : numberPro.size());
        map.put("numberSes", numberSes == null ? 0 : numberSes.size());
        map.put("numberError", numberEro == null ? 0 : numberEro.size());
        return map;
    }

}
